<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogFormVisible = true"
          >添加分类</el-button
        >
        <!-- 添加分类的对话框 -->
        <el-dialog
          title="添加文章分类"
          :visible.sync="dialogFormVisible"
          width="35%"
          @closed="$refs.form.resetFields()"
        >
          <el-form
            :model="form"
            :rules="addRules"
            ref="form"
            label-width="70px"
          >
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="form.cate_name" minlength="1"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input
                v-model="form.cate_alias"
                minlength="1"
                maxlength="15"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCateHandler">添 加</el-button>
          </div>
        </el-dialog>
        <!-- 修改分类的对话框 -->
        <el-dialog
          title="修改文章分类"
          :visible.sync="editVisible"
          width="35%"
          @closed="$refs.editform.resetFields()"
        >
          <el-form
            :model="editform"
            :rules="addRules"
            ref="editform"
            label-width="70px"
          >
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="editform.cate_name" ></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input
                v-model="editform.cate_alias"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="editVisible = false">取 消</el-button>
            <el-button type="primary" @click="editCateHandler">修 改</el-button>
          </div>
        </el-dialog>
      </div>
      <!-- 表格区域 -->
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column label="序号" width="100" type="index">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
          <el-button type="primary" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="remove(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      dialogFormVisible: false,
      editVisible: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      },
      editform: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  methods: {
    async initCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code === 0) this.cateList = res.data
    },
    addCateHandler () { // 添加
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.form)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.initCateList()
        this.dialogFormVisible = false
      })
    },
    showEditDialog (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许删除这个分类')
      this.editform = { ...row }
      this.editVisible = true
    },
    editCateHandler () {
      this.$refs.editform.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editform)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.initCateList()
        this.editVisible = false
      })
    },
    async remove (id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除这个分类')
      const confirmResult = await this.$confirm('此操作将永久删除该分类,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(e => e)
      if (confirmResult === 'cancel') return
      const { data: res } = await this.$http.delete('/my/cate/del', {
        params: { id }
      })
      // 判断是否删除成功
      if (res.code !== 0) return this.$message.error('删除分类失败！')
      this.$message.success('删除分类成功！')
      this.initCateList()
    }
  },
  created () {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
